﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Hosting;

namespace GPSTrackingWebsiteManager.StockFilesManagerService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StockFilesManagerService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select StockFilesManagerService.svc or StockFilesManagerService.svc.cs at the Solution Explorer and start debugging.
    public class StockFilesManagerService : IStockFilesManagerService
    {
        public byte[] GetStockFile(int companyID, int stockFileID) 
        {
            GPSTrackingDBV2.Entities.Companies companyEntity = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.GetByCompanyId(companyID);
            GPSTrackingDBV2.Entities.Stock stockEntity = GPSTrackingDBV2.Data.DataRepository.StockProvider.GetByCompanyIdStockId(companyID, stockFileID);

            string Path = "~/PointOfSale/StockPDF/" + companyEntity.Guid + "/" + stockEntity.PdfDetailUrl;

            byte[] bytes = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath(Path));

            return bytes;

        }

        public void DeleteStockItems(int companyID, string imageName, string pdfName)
        {
            GPSTrackingDBV2.Entities.Companies companyEntity = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.GetByCompanyId(companyID);

            string pathImage = "~/PointOfSale/StockItems/" + companyEntity.Guid + "/" + imageName;
            string pathPdf = "~/PointOfSale/StockPDF/" + companyEntity.Guid + "/" + pdfName;

            if (!String.IsNullOrEmpty(imageName))
                System.IO.File.Delete(HostingEnvironment.MapPath(pathImage));

            if (!String.IsNullOrEmpty(imageName))
                System.IO.File.Delete(HostingEnvironment.MapPath(pathPdf));
        }

        public byte[] GetCompanyLogo(int companyID)
        {
            return GetCompanyLogoByCompanyID(companyID);
        }

        public byte[] GetCompanyLogoBySerial(string serial)
        {
            GPSTrackingDBV2.Data.CompaniesQuery companiesQuery = new GPSTrackingDBV2.Data.CompaniesQuery();
             companiesQuery.AppendEquals(GPSTrackingDBV2.Entities.CompaniesColumn.Guid, serial);

           int companyID = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.Find(companiesQuery).FirstOrDefault().CompanyId;

            return GetCompanyLogoByCompanyID(companyID);

        }

        private byte[] GetCompanyLogoByCompanyID(int companyId)
        {
            GPSTrackingDBV2.Entities.Companies companyEntity = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.GetByCompanyId(companyId);
            System.Drawing.Image image = System.Drawing.Image.FromFile(HostingEnvironment.MapPath("~/Template/Companies/" + companyEntity.Logo));

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            return memoryStream.ToArray();
        }


        public byte[] GetStockImage(int companyID, int stockID)
        {
            byte[] bytes = new byte[0];
            try
            {
                GPSTrackingDBV2.Entities.Companies companyEntity = GPSTrackingDBV2.Data.DataRepository.CompaniesProvider.GetByCompanyId(companyID);
                GPSTrackingDBV2.Entities.Stock stockEntity = GPSTrackingDBV2.Data.DataRepository.StockProvider.GetByCompanyIdStockId(companyID, stockID);

                string Path = "~/PointOfSale/StockItems/" + companyEntity.Guid + "/" + stockEntity.ImageUrl;

                bytes = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath(Path));

                return bytes;
            }
            catch
            {
                return bytes;
            }
        }

    }
}
